package app.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.apache.log4j.Logger;

import app.action.NumberDocument;
import app.action.StartServer;

public class StartServerUi extends AbsCommonPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -376980906238665148L;
	private final static Logger logger = Logger.getLogger(StartServerUi.class);

	protected final String[] texts = { "Start Server", "Clear log", "Host :", "Port :", "Number of connected clients: " };

	protected JLabel jLabel1;
	protected JLabel jLabel2;
	protected JLabel jLabel3;
	protected JTextArea jTextArea = new JTextArea(); // For displaying log
	protected JScrollPane jScrollPane; // For displaying log
	protected JTextField host;// host
	protected JTextField port;// port
	protected JButton btnStartServer;
	protected JButton btnClearLog;

	private StartServer server;

	public StartServerUi(AbsMainFrame mFrame) {
		super(mFrame);
	}

	@Override
	protected void addComponents() {
		add(host = new JTextField());
		add(port = new JTextField(new NumberDocument(), "4444", 10));
		add(jLabel1 = new JLabel(texts[2]));
		add(jLabel2 = new JLabel(texts[3]));
		add(jLabel3 = new JLabel(texts[4]));
		add(btnStartServer = new JButton(texts[0]));
		add(btnClearLog = new JButton(texts[1]));
		add(jScrollPane = new JScrollPane(jTextArea));
	}

	@Override
	protected void initializeComponents() {
		// jTextField1 - Host
		host.setEditable(false);
		host.setText("localhost");

		// btnStartServer
		btnStartServer.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				doStartServer(e);
			}
		});

		// btnClearLog
		btnClearLog.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				jTextArea.setText("");
			}
		});

		// jLabel3
	}

	@Override
	protected void resizeComponents() {
		int sep = 4;
		int x = 50;
		int y = 25;
		int width = btnWid / 3;
		int height = btnHei;

		// jLabel1
		jLabel1.setBounds(x, y, width, height);

		// jLabel2
		y += height + sep;
		jLabel2.setBounds(x, y, width, height);

		// jTextField1
		x += width + sep;
		width = btnWid * 2;
		y = 25;
		host.setBounds(x, y, width, height);

		// jTextField2
		y += height + sep;
		width = (int) (btnWid * 0.65);
		port.setBounds(x, y, width, height);

		// jLabel3
		x += width + 3 * sep;
		width = (int) (2.5 * btnWid);
		jLabel3.setBounds(x, y, width, height);

		// jButton1
		x = host.getX() + host.getWidth() + sep;
		y = 25;
		width = btnWid;
		height = btnHei;
		btnStartServer.setBounds(x, y, width, height);

		// jButton2
		width = 80;
		x = this.getWidth() - sep - width;
		y = port.getY();
		btnClearLog.setBounds(x, y, width, height);

		// logScreen
		y += height * 1.5 + sep;
		x = sep;
		width = this.getWidth() - 2 * sep;
		height = this.getHeight() - y - sep;
		jScrollPane.setBounds(x, y, width, height);
		jScrollPane.updateUI();
	}

	private void doStartServer(ActionEvent e) {
		if(server != null) {
			return;
		}

		try {
			logger.info("Server start with host = \"" + host.getText() + "\", and port = " + port.getText());
			server = new StartServer(Integer.valueOf(port.getText()));
			server.start();

			port.setEditable(false);
			btnStartServer.setEnabled(false);
		}catch (NumberFormatException | IOException e1) {
			logger.error(e1.getMessage(), e1.getCause());
		}
	}

	@Override
	public void close(boolean removeThis) {
		// TODO finalize when closing
		super.close(removeThis);
	}
};
